package co.y.t.hyfPay;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.TreeMap;


/**
 * 无卡快捷消费servlet
 * @author zhangyu
 *
 */
public class QuickPayServlet extends HttpServlet {

	private static final long serialVersionUID = -2140049415017110583L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("GBK");
		// 网页-无卡支付
		String payType = "016";
		long amount = (long) (Double.parseDouble(request.getParameter("amount"))*100.0);
		// POST请求参数：按签名顺序，有序存放
		TreeMap<String, String> params = new TreeMap<String, String> ();
		
		params.put("version",Config.version);
		params.put("organNo",Config.organNo);
		params.put("hicardMerchNo",Config.merchNo);
		params.put("payType",payType);	
		params.put("bizType","账户充值");																			// 商家自定义的业务类别	
		params.put("merchOrderNo", Long.toString(System.currentTimeMillis()));	// 商家自定义的订单号
		params.put("showPage","0");																			// 固定
		params.put("amount", Long.toString(amount));											// 金额单位为“分”
		String returnUrl = Config.returnUrl;											// 前台返回页面：以商家自己的网站为准
		String notifyUrl = Config.notifyUrl;											// 后端通知页面：以商家自己的网站为准
		params.put("frontEndUrl",returnUrl);
		params.put("backEndUrl",notifyUrl);	
		params.put("sign", Safe.sign(params));															// 签名
		params.put("remark","备注");																			// 该字段不需参加签名
		params.put("reserved","预留空间");																	// 该字段不需参加签名
		params.put("goodsName","账户充值");						// 商家自定义的货物名称，该字段不需参加签名
		
		String json = new ObjectMapper().writeValueAsString(params);
		System.out.println("发送数据：" + json);
		String url = "http://www.icloudful.com/hicardpay/order/create";
		//String url = "http://" + Config.payIPAddress +  "/hicardpay/order/create";
		String resp = HttpClient.post(json, url);
		System.out.println("返回数据：" + resp);
		response.setContentType("text/html; charset=GBK");
		response.getWriter().write(resp);
	}
	
}
